package copyengine.ui.component.list.impl
{
	import flash.display.MovieClip;
	import flash.geom.Rectangle;

	import copyengine.ui.component.list.CEMListCore;
	import copyengine.ui.component.list.ICEMListCalculateHelper;

	public final class CEMListFixedCellRenderCalculateHelper implements ICEMListCalculateHelper
	{
		private var direction:int;
		private var viewportBoundSize:int;

		private var cellRenderFixedPos:int;

		private var cellRenderOffsetX:int;
		private var cellRenderOffsetY:int;
		private var cellRenderBoundSize:int;

		private var cellRenderGap:int;

		private var totalCellRenderNum:int;

		private var dataProvier:Array;

		private var allLayoutQueueDataVector:Vector.<CEMListLayoutQueueData>;

		public function CEMListFixedCellRenderCalculateHelper()
		{
		}

		public function initialize(_targetMc:MovieClip, _direction:int, _totalCellRenderNum:int):void
		{
			direction=_direction;
			viewportBoundSize=direction == CEMListCore.LAYOUT_HORIZONTAL ? _targetMc.width : _targetMc.height;
			totalCellRenderNum=_totalCellRenderNum;

			//-------------------------
			//--初始化QueueData
			//-------------------------
			allLayoutQueueDataVector=new Vector.<CEMListLayoutQueueData>();
			for (var i:int=0; i < totalCellRenderNum; i++)
			{
				var queueData:CEMListLayoutQueueData=new CEMListLayoutQueueData();
				allLayoutQueueDataVector.push(queueData);
			}

			//----------------------------------
			//--计算出CellRender之间的间距
			//----------------------------------
			if (_targetMc["c1"] != null)
			{
				cellRenderGap=direction == CEMListCore.LAYOUT_HORIZONTAL ? _targetMc["c1"].x - _targetMc["c0"].x - _targetMc["c0"].width : _targetMc["c1"].y - _targetMc["c0"].y - _targetMc["c0"].height;
			}
			else
			{
				cellRenderGap=0;
			}


			//------------------------------
			//--计算出CellRender的Offset
			//--和每个CellRender的BoundSize
			//-----------------------------
			var cellRenderMc:MovieClip=_targetMc["c0"];
			var cellRenderRect:Rectangle=cellRenderMc.getBounds(cellRenderMc);

			cellRenderFixedPos=direction == CEMListCore.LAYOUT_HORIZONTAL ? cellRenderMc.y : cellRenderMc.x;
			cellRenderBoundSize=direction == CEMListCore.LAYOUT_HORIZONTAL ? cellRenderMc.width : cellRenderMc.height;
			cellRenderOffsetX=cellRenderRect.x;
			cellRenderOffsetY=cellRenderRect.y;
		}

		public function setDataProvier(_dataProvier:Array):void
		{
			dataProvier=_dataProvier;
		}


		public function getCalculatedLayoutQueue(_scrollPosition:int):Vector.<CEMListLayoutQueueData>
		{
			var queueData:CEMListLayoutQueueData;

			//----------------------------------------------------
			//--先将所有QueueData设置为isLayouted=false
			//-----------------------------------------------------
			for each (queueData in allLayoutQueueDataVector)
			{
				queueData.isLayouted=false;
			}

			//-----------------------------------
			//--1·如果scrollPosition超出最大位置,及没有任何CellRender会在屏幕上面显示出来
			//--2·如果scrollPosition小于0则第一个渲染的scrollPosition一定是 0 号,但并不能保证0号元素会在屏幕上渲染出来,需要执行后续逻辑判断
			//--3·普通情况下及根据scrollPosition排序每个CellRender的位置
			//-----------------------------------
			if (_scrollPosition > dataProvier.length * (cellRenderBoundSize + cellRenderGap) - cellRenderGap)
			{
				//所有的CellRender都不在屏幕内部
				for each (queueData in allLayoutQueueDataVector)
				{
					queueData.isEnable=false;
				}
			}
			else
			{
				var firstInViewCellRenderIndex:int;
				var startLayoutPos:int
				if (_scrollPosition < 0)
				{
					firstInViewCellRenderIndex=0;
					startLayoutPos=-_scrollPosition;
				}
				else
				{
					firstInViewCellRenderIndex=Math.floor((_scrollPosition + cellRenderGap) / (cellRenderBoundSize + cellRenderGap));
					startLayoutPos=firstInViewCellRenderIndex * (cellRenderGap + cellRenderBoundSize) - _scrollPosition;
				}

				//当此变量一旦被设置，后续的CellRender也不会在屏幕上显示,直接isEnable=false即可
				var isOutOfView:Boolean=false;

				for (var i:int=0; i < totalCellRenderNum; i++)
				{
					queueData=allLayoutQueueDataVector[i];
					queueData.cellRenderIndex=firstInViewCellRenderIndex + i;
					if (queueData.cellRenderIndex >= dataProvier.length)
					{
						queueData.isEnable=false;
					}
					else
					{
						if (isOutOfView)
						{
							queueData.isEnable=false;
						}
						else
						{
							var currentCellRenderViewPos:int=startLayoutPos + i * (cellRenderBoundSize + cellRenderGap);
							isOutOfView=currentCellRenderViewPos > viewportBoundSize;
							if (isOutOfView)
							{
								queueData.isEnable=false;
							}
							else
							{
								queueData.isEnable=true;
								setQueueDataLayoutPos(queueData, currentCellRenderViewPos);
							}
						}
					}
				}
			}
			return allLayoutQueueDataVector;
		}

		public function getMaxScrollPosition():int
		{
			return Math.max(0, dataProvier.length * cellRenderBoundSize + (dataProvier.length - 1) * cellRenderGap - viewportBoundSize);
		}


		//========================//
		//==Private  Function
		//=========================//
		private function setQueueDataLayoutPos(_queueData:CEMListLayoutQueueData, _layoutPos:int):void
		{
			if (direction == CEMListCore.LAYOUT_HORIZONTAL)
			{
				_queueData.layoutPosX=_layoutPos - cellRenderOffsetX;
				_queueData.layoutPosY=cellRenderFixedPos;
			}
			else
			{
				_queueData.layoutPosX=cellRenderFixedPos;
				_queueData.layoutPosY=_layoutPos - cellRenderOffsetY;
			}
		}

	}
}
